package com.ahwei.product.service.impl;

import com.ahwei.common.rest.base.PageVo;
import com.ahwei.common.rest.util.PageVoUtils;
import com.ahwei.product.entity.Product;
import com.ahwei.product.mapper.ProductMapper;
import com.ahwei.product.service.ProductService;
import com.ahwei.product.vo.ProductQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ahwei
 * @since 2022-05-25
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void reduceInventory(Long productId, Integer quantity) {
        Product product = this.getById(productId);
        if (product.getStock() < quantity) {
            throw new RuntimeException("库存不足");
        }
        //减库存
        product.setStock(product.getStock() - quantity);
        this.updateById(product);
    }

    @Override
    public PageVo<Product> listPage(IPage<Product> pageParam,ProductQueryVo queryVo) {
        //避免我们在代码中写类似的于"name"的硬编码
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();

        //设置排序
        //queryWrapper.orderByAsc(Product::getSort);
        //如果查询条件对象为空，直接查询
        if (PageVoUtils.objectFieldIsEmpty(queryVo)) {
            //查询数据库 * 设置数据到分页对象
            return PageVoUtils.createPageVo(page(pageParam, queryWrapper));
        }
        //获取查询参数
        String name = queryVo.getName();
        String joinDateBegin = queryVo.getJoinDateBegin();
        String joinDateEnd = queryVo.getJoinDateEnd();

        // 不为空才添加查询条件   ge(>=)、gt(>)、le(<=)、lt(<)
        if (ObjectUtils.isNotEmpty(name)) {
            queryWrapper.like(Product::getName, name);
        }
        if (ObjectUtils.isNotEmpty(joinDateBegin)) {
            queryWrapper.ge(Product::getGmtCreate, joinDateBegin + " 00:00:00");
        }
        if (ObjectUtils.isNotEmpty(joinDateEnd)) {
            queryWrapper.le(Product::getGmtCreate, joinDateEnd + " 23:59:59");
        }

        /*设置数据到分页对象*/
        return PageVoUtils.createPageVo(page(pageParam, queryWrapper));
    }



    @Transactional(rollbackFor = Exception.class)
    @Override
    public Product getById(Serializable id) {
        return getBaseMapper().selectById(id);
    }

    @Override
    public boolean removeByIds(Collection<?> list) {
        return !CollectionUtils.isEmpty(list) && SqlHelper.retBool(this.getBaseMapper().deleteBatchIds(list));
    }

    @Override
    public boolean updateById(Product entity) {
        return SqlHelper.retBool(this.getBaseMapper().updateById(entity));
    }
}
